from   myapp                         import app
from   behave                        import *
from   selenium.webdriver.common.by  import By
from   selenium.webdriver.support.ui import WebDriverWait
from   selenium.webdriver.support    import expected_conditions as EC
from   selenium.common.exceptions    import TimeoutException
import traceback

exec(open(app.root_path + '/constants/tests_constants.py').read())


@given(u'I am on the notabug home page')
def am_on_homepage(context):
  context.execute_steps('when I browse to "' + NOTABUG_HOME_URL + '"')


@then(u'I should be on the notabug home page')
def should_be_on_home_page(context):
  home_content = context.browser.find_element_by_id(NOTABUG_HOME_TITLE_H2_ID)
  assert context.browser.current_url == NOTABUG_HOME_URL
  assert context.browser.title       == NOTABUG_HOME_TITLE
  assert NOTABUG_HOME_TITLE_H2_TEXT  == home_content.text


@when(u'I login to notabug with bogus credentials')
def submit_login_form(context):
  context.browser.find_element_by_id(NOTABUG_LOGIN_INPUT_ID).send_keys('abc')
  context.browser.find_element_by_id(PASS_INPUT_ID ).send_keys('xyz')
  context.browser.find_element_by_id(LOGIN_BTN_ID  ).click()


@then(u'I should be on the notabug login page')
def should_be_on_login_page(context):
  try: WebDriverWait(context.browser , 35).until(EC.title_contains(NOTABUG_LOGIN_TITLE))
  except(TimeoutException): traceback.print_exc()
  context.execute_steps('then I should be on the "' + NOTABUG_LOGIN_URL + '" webpage')


@then(u'I should see the notabug login page again with an error notice')
def should_see_login_error(context):
  try: error_div = WebDriverWait(context.browser , 35).until(
         EC.presence_of_element_located((By.CSS_SELECTOR , LOGIN_ERROR_CSS))
       )
  except(TimeoutException): traceback.print_exc()

  assert context.browser.current_url == NOTABUG_LOGIN_URL
  assert context.browser.title       == NOTABUG_LOGIN_TITLE
  assert NAB_LOGIN_ERROR_TEXT        == error_div.text
